// HSS.Logging.Management.DatabaseInstaller.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DatabaseInstaller.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Logging.Management
{
	#region Using Directives
	using System;
	using System.Security.Permissions;
	using System.Text;
	using System.Windows.Forms;
	using HSS.Forms;
	#endregion

	#region DatabaseInstaller
	/// <summary>
	/// Provide the ability to install/uninstall the HSS Core Database
	/// </summary>
	public static class DatabaseInstaller
	{
		internal const string LOCAL_MACHINE = "127.0.0.1";

		/// <summary>
		/// Run the Install Wizard
		/// </summary>
		[STAThread()]
#if !DEBUG
		[PrincipalPermissionAttribute(SecurityAction.Demand, Role = "Administrators")]
#endif
		public static void Run()
		{
			using (var ctx = new AppContext<InstallWizard>())
			{
				ctx.RestoreLocation = false;
				ctx.RestoreSize = false;
				ctx.RestoreState = false;
				ctx.Run(null, InitApp);
			}
		}
		private static InstallWizard InitApp(string[] args, AppContext<InstallWizard> ctx)
		{
			ctx.ShowError += new EventHandler<SendExceptionEventArgs>(App_ShowError);
			ctx.SendException += new EventHandler<SendExceptionEventArgs>(App_SendException);
			return new InstallWizard();
		}
		private static void App_ShowError(object sender, SendExceptionEventArgs e)
		{
			MessageBox.Show(e.UnhandledException.ToString(), "Exception Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
		}
		private static void App_SendException(object sender, SendExceptionEventArgs e)
		{
			if (e.SendException)
				SendException(e.UnhandledException);

			if (e.RestartApp)
				Application.Restart();
		}
		private static void SendException(Exception ex)
		{
			SendException(ex.ToString());
		}
		private static void SendException(string message)
		{
			System.Threading.ThreadPool.QueueUserWorkItem(SendException, message);
		}
		private static void SendException(object objMessage)
		{
			try
			{
				var builder = new StringBuilder(objMessage.ToString());
				builder.AppendLine();
				builder.AppendLine();
				AppContext<InstallWizard>.AppendAppContextInfo(builder);
				builder.AppendLine();
				builder.AppendLine();
				AppContext<InstallWizard>.AppendEnvironmentInfo(builder);
				var subject = Application.ProductName + ": " + HSS.Logging.Management.Resources.supportSubject;
				var msg = new System.Net.Mail.MailMessage(HSS.Logging.Management.Resources.suppportEmail, HSS.Logging.Management.Resources.suppportEmail, subject, builder.ToString());
				HSS.Net.Mail.SmtpClient.SendEmail(msg);
			}
			catch { }
		}

		/// <summary>
		/// Show the Install Wizard
		/// </summary>
		/// <param name="owner">The Owning form</param>
#if !DEBUG
		[PrincipalPermissionAttribute(SecurityAction.Demand, Role = "Administrators")]
#endif
		public static void Show(IWin32Window owner)
		{
			Show(owner, true);
		}
		/// <summary>
		/// Show the Install Wizard
		/// </summary>
		/// <param name="owner">The Owning form</param>
		/// <param name="installOption">Desired option is to install or uninstall</param>
#if !DEBUG
		[PrincipalPermissionAttribute(SecurityAction.Demand, Role = "Administrators")]
#endif
		public static void Show(IWin32Window owner, bool installOption)
		{
			using (InstallWizard wiz = new InstallWizard(installOption))
				wiz.ShowDialog(owner);
		}
		/// <summary>
		/// Show the Install Wizard
		/// </summary>
		/// <param name="owner">The Owning form</param>
		/// <param name="installOption">Desired option is to install or uninstall</param>
		/// <param name="connectionString">The pre-existing connection string</param>
#if !DEBUG
		[PrincipalPermissionAttribute(SecurityAction.Demand, Role = "Administrators")]
#endif
		public static void Show(IWin32Window owner, bool installOption, string connectionString)
		{
			using (InstallWizard wiz = new InstallWizard(installOption, connectionString))
				wiz.ShowDialog(owner);
		}
	}
	#endregion
}